home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ue312src.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1993-04-20  |  27KB  |  1,150 lines

  1. /*
  2.  *    MicroEMACS 3.12
  3.  *        written by Daniel M. Lawrence
  4.  *        based on code by Dave G. Conroy.
  5.  *
  6.  *    (C)Copyright 1988,1989,1990,1991,1992,1993 by Daniel M. Lawrence
  7.  *    MicroEMACS 3.12 can be copied and distributed freely for any
  8.  *    non-commercial purposes. MicroEMACS 3.12 can only be incorporated
  9.  *    into commercial software with the permission of the current author.
  10.  *
  11.  * This file contains the main driving routine, and some keyboard processing
  12.  * code, for the MicroEMACS screen editor.
  13.  *
  14.  */
  15.  
  16. #include    <stdio.h>
  17.  
  18. /* make global definitions not external */
  19. #define maindef
  20.  
  21. #include    "estruct.h"    /* global structures and defines */
  22. #include    "eproto.h"    /* variable prototype definitions */
  23. #include    "efunc.h"    /* function declarations and name table */
  24. #include    "edef.h"    /* global definitions */
  25. #include    "elang.h"    /* human language definitions */
  26. #include    "ebind.h"    /* default key bindings */
  27.  
  28. /* for many different systems, increase the default stack space */
  29.  
  30. #if    MSDOS && MSC
  31. #if    _MSC_VER < 700
  32. unsigned _stackavail = 40000;
  33. #endif
  34. #endif
  35.  
  36. #if    MSDOS && LATTICE
  37. unsigned _stack = 20000;
  38. #endif
  39.  
  40. #if    MSDOS && (DTL | ZTC)
  41. int    _okbigbuf = 0;        /* Only allocate memory when needed.*/
  42. int    _stack = 20000;     /* Reset the ol' stack size.*/
  43. #endif
  44.  
  45. #if    TOS && MWC
  46. long _stksize = 20000L;     /* reset stack size (must be even) */
  47. #endif
  48.  
  49. #if    MSDOS && AZTEC
  50. int _STKSIZ = 20000/16;     /* stack size in paragraphs */
  51. int _STKRED = 1024;        /* stack checking limit */
  52. int _HEAPSIZ = 4096/16;     /* (in paragraphs) */
  53. /*int _STKLOW = 0;        default is stack above heap (small only) */
  54. #endif
  55.  
  56. #if    MSDOS && TURBO
  57. extern unsigned int _stklen = 10000;
  58. #endif
  59.  
  60. /*    make VMS happy...    */
  61.  
  62. #if    VMS
  63. #include    <ssdef.h>
  64. #define GOOD    (SS$_NORMAL)
  65. #endif
  66.  
  67. #ifndef GOOD
  68. #define GOOD    0
  69. #endif
  70.  
  71. /*
  72.     This is the primary entry point that is used by command line
  73.     invocation, and by applications that link with microemacs in
  74.     such a way that each invocation of Emacs is a fresh environment.
  75.  
  76.     There is another entry point in VMS.C that is used when
  77.     microemacs is "linked" (In quotes, because it is a run-time link
  78.     rather than a link-time link.) with applications that wish Emacs
  79.     to preserve it's context across invocations.  (For example,
  80.     EMACS.RC is only executed once per invocation of the
  81.     application, instead of once per invocation of Emacs.)
  82.  
  83.     Note that re-entering an Emacs that is saved in a kept
  84.     subprocess would require a similar entrypoint.
  85. */
  86.  
  87. #if    CALLED
  88. int emacs(argc, argv)
  89. #else
  90. main(argc, argv)
  91. #endif
  92.  
  93. int argc;    /* # of arguments */
  94. char *argv[];    /* argument strings */
  95.  
  96. {
  97.     register int status;
  98.  
  99.     /* Initialize the editor */
  100.     eexitflag = FALSE;
  101. #if    !WINDOW_MSWIN
  102.     vtinit();        /* Terminal */
  103. #endif
  104.  
  105.     if (eexitflag)
  106.         goto abortrun;
  107.     edinit(mainbuf);      /* Buffers, windows, screens */
  108.  
  109.     varinit();        /* user variables */
  110.     initchars();        /* character set definitions */
  111.  
  112.     /* Process the command line and let the user edit */
  113. #if    VMS
  114.     expandargs(&argc, &argv);    /* expand VMS wildcards.*/
  115. #endif
  116.     dcline(argc, argv, TRUE);
  117.     status = editloop();
  118. abortrun:
  119.     vttidy();
  120. #if    CLEAN
  121.     clean();
  122. #endif
  123. #if    CALLED
  124.     return(status);
  125. #else
  126.     exit(status);
  127. #endif
  128. }
  129.  
  130. #if    CLEAN
  131. /*
  132.     On some primitive operation systems, and when emacs is used as
  133.     a subprogram to a larger project, emacs needs to de-alloc its
  134.     own used memory, otherwise we just exit.
  135. */
  136.  
  137. PASCAL NEAR clean()
  138. {
  139.     register BUFFER *bp;    /* buffer list pointer */
  140.     register SCREEN *scrp;    /* ptr to screen to dump */
  141.  
  142.     /* first clean up the screens */
  143.     scrp = first_screen;
  144.     while (scrp) {
  145.         first_screen = scrp->s_next_screen;
  146.         free_screen(scrp);
  147.         scrp = first_screen;
  148.     }
  149.     wheadp = NULL;
  150.  
  151.     /* then the buffers */
  152.     bp = bheadp;
  153.     while (bp) {
  154.         bp->b_nwnd = 0;
  155.         bp->b_flag = 0; /* don't say anything about a changed buffer! */
  156.         zotbuf(bp);
  157.         bp = bheadp;
  158.     }
  159.  
  160.     /* and the kill buffers */
  161.     clear_ring(FALSE, 0);
  162.  
  163.     /* clear some search variables */
  164. #if    MAGIC
  165.     mcclear();
  166.     rmcclear();
  167. #endif
  168.     if (patmatch != NULL) {
  169.         free(patmatch);
  170.         patmatch = NULL;
  171.     }
  172.  
  173.     /* dealloc the user variables */
  174.     varclean();
  175.  
  176.     /* and the video buffers */
  177.     vtfree();
  178. }
  179. #endif
  180.  
  181. /*    Process a command line.   May be called any time.    */
  182.  
  183. PASCAL NEAR dcline(argc, argv, firstflag)
  184.  
  185. int argc;
  186. char *argv[];
  187. int firstflag;    /* is this the first time in? */
  188.  
  189. {
  190.     register BUFFER *bp;        /* temp buffer pointer */
  191.     register int    firstfile;    /* first file flag */
  192.     register int    carg;        /* current arg to scan */
  193.     register int    startflag;    /* startup executed flag */
  194.     BUFFER *firstbp = NULL;     /* ptr to first buffer in cmd line */
  195.     int viewflag;            /* are we starting in view mode? */
  196.     int gotoflag;            /* do we need to goto a line at start? */
  197.     int gline;            /* if so, what line? */
  198.     int gchar;            /* and what character? */
  199.     int searchflag;         /* Do we need to search at start? */
  200.     int errflag;            /* C error processing? */
  201.     VDESC vd;            /* variable num/type */
  202.     char bname[NBUFN];        /* buffer name of file to read */
  203. #if    MSDOS | OS2
  204.     unsigned char *scan;        /* scan pointer for filenames */
  205. #endif
  206.     
  207. #if    CRYPT
  208.     int cryptflag;            /* encrypting on the way in? */
  209.     char ekey[NPAT];        /* startup encryption key */
  210. #endif
  211.         NOSHARE CONST extern char *pathname[]; /* startup file path/name array */
  212.  
  213.     viewflag = FALSE;    /* view mode defaults off in command line */
  214.     gotoflag = FALSE;    /* set to off to begin with */
  215.     gline = 1; gchar = 1;    /* line and character to go to */
  216.     searchflag = FALSE;    /* set to off to begin with */
  217.     firstfile = TRUE;    /* no file to edit yet */
  218.     startflag = FALSE;    /* startup file not executed yet */
  219.     errflag = FALSE;    /* not doing C error parsing */
  220.     exec_error = FALSE;    /* no macro error pending */
  221. #if    CRYPT
  222.     cryptflag = FALSE;    /* no encryption by default */
  223. #endif
  224.     disphigh = FALSE;    /* don't escape high bit characters */
  225.     lterm[0] = 0;        /* standard line terminators */
  226.  
  227.     /* Parse a command line */
  228.     for (carg = 1; carg < argc; ++carg) {
  229.  
  230.         /* Process Switches */
  231. #if WMCS
  232.         if (argv[carg][0] == ':') {
  233. #else
  234.         if (argv[carg][0] == '-') {
  235. #endif
  236.             switch (argv[carg][1]) {
  237.                 /* Process Startup macroes */
  238.                 case 'c':    /* -c for changable file */
  239.                 case 'C':
  240.                     viewflag = FALSE;
  241.                     break;
  242.                 case 'e':    /* -e process error file */
  243.                 case 'E':
  244.                     errflag = TRUE;
  245.                     break;
  246.                 case 'g':    /* -g for initial goto line */
  247.                 case 'G':
  248.                     gotoflag = TRUE;
  249.                     gline = asc_int(&argv[carg][2]);
  250.                     break;
  251.                 case 'i':    /* -i<var> <value> set an initial */
  252.                 case 'I':    /* value for a variable */
  253.                     bytecopy(bname, &argv[carg][2], NVSIZE);
  254.                     findvar(bname, &vd, NVSIZE + 1);
  255.                     if (vd.v_type == -1) {
  256.                         mlwrite(TEXT52, bname);
  257. /*                            "%%No such variable as '%s'" */
  258.                         break;
  259.                     }
  260.                     svar(&vd, argv[++carg]);
  261.                     break;
  262. #if    CRYPT
  263.                 case 'k':    /* -k<key> for code key */
  264.                 case 'K':
  265.                     cryptflag = TRUE;
  266.                     strcpy(ekey, &argv[carg][2]);
  267.                     break;
  268. #endif
  269.                 case 'p':    /* -p for initial goto char position */
  270.                 case 'P':
  271.                     gotoflag = TRUE;
  272.                     gchar = asc_int(&argv[carg][2]);
  273.                     break;
  274.                 case 'r':    /* -r restrictive use */
  275.                 case 'R':
  276.                     restflag = TRUE;
  277.                     break;
  278.                 case 's':    /* -s for initial search string */
  279.                 case 'S':
  280.                     searchflag = TRUE;
  281.                     bytecopy(pat,&argv[carg][2],NPAT);
  282.                     setjtable();
  283.                     break;
  284.                 case 'v':    /* -v for View File */
  285.                 case 'V':
  286.                     viewflag = TRUE;
  287.                     break;
  288.                 default:    /* unknown switch */
  289.                     /* ignore this for now */
  290.                     break;
  291.             }
  292.  
  293.         } else if (argv[carg][0] == '+') {    /* +<line num> */
  294.  
  295.             gotoflag = TRUE;
  296.             gline = asc_int(&argv[carg][1]);
  297.  
  298.         } else if (argv[carg][0]== '@') {
  299.  
  300.             /* Process Startup macroes */
  301.             if (startup(&argv[carg][1]) == TRUE)
  302.                 /* don't execute emacs.rc */
  303.                 startflag = TRUE;
  304.  
  305. #if WINDOW_MSWIN32
  306.         } else if ((argv[carg][0] != ' ') ||
  307.                            (argv[carg][1] != '\0')) {
  308.             /* WinNT PDK2 causes spurious space arguments */
  309. #else
  310.         } else {
  311. #endif
  312.             /* Process an input file */
  313. #if    MSDOS | OS2
  314.             /* change forward slashes to back */
  315.             scan = argv[carg];
  316.             while (*scan) {
  317.                 if (*scan == '/')
  318.                     *scan = DIRSEPCHAR;
  319.                 ++scan;
  320.             }
  321. #endif
  322.             /* set up a buffer for this file */
  323.             makename(bname, argv[carg]);
  324.             unqname(bname);
  325.  
  326.             /* set this to inactive */
  327.             bp = bfind(bname, TRUE, 0);
  328.             strcpy(bp->b_fname, argv[carg]);
  329. #if    WINDOW_MSWIN
  330.             fullpathname (bp->b_fname, NFILEN);
  331. #endif
  332.             bp->b_active = FALSE;
  333.             if (firstfile) {
  334.                 firstbp = bp;
  335.                 firstfile = FALSE;
  336.             }
  337.  
  338.             /* set the modes appropriatly */
  339.             if (viewflag)
  340.                 bp->b_mode |= MDVIEW;
  341. #if    CRYPT
  342.             if (cryptflag) {
  343.                 bp->b_mode |= MDCRYPT;
  344.                 crypt((char *)NULL, 0);
  345.                 crypt(ekey, strlen(ekey));
  346.                 bytecopy(bp->b_key, ekey, NPAT);
  347.             }
  348. #endif
  349.         }
  350.     }
  351.  
  352.     /* if we are C error parsing... run it! */
  353.     if (errflag) {
  354.         if (startup("error.cmd") == TRUE)
  355.             startflag = TRUE;
  356.     }
  357.  
  358.     /* if invoked with no other startup files,
  359.        run the system startup file here */
  360.     if (firstflag && startflag == FALSE)
  361.         startup("");
  362.  
  363.     /* if there are any files to read, read the first one! */
  364.     if (firstflag) {
  365.         bp = bfind(mainbuf, FALSE, 0);
  366.         if (firstfile == FALSE && (gflags & GFREAD)) {
  367.             swbuffer(firstbp);
  368.             curbp->b_mode |= gmode;
  369.             update(TRUE);
  370.             mlwrite(lastmesg);
  371.             zotbuf(bp);
  372.         } else
  373.             bp->b_mode |= gmode;
  374.     } else {
  375.         swbuffer(firstbp);
  376.         curbp->b_mode |= gmode;
  377.         update(TRUE);
  378.         mlwrite(lastmesg);
  379.     }
  380.  
  381.     /* Deal with startup gotos and searches */
  382.     if (gotoflag && searchflag) {
  383.         update(FALSE);
  384.         mlwrite(TEXT101);
  385. /*            "[Can not search and goto at the same time!]" */
  386.     }
  387.     else if (gotoflag) {
  388.         if ((gotoline(TRUE, gline) == FALSE) ||
  389.             (forwchar(TRUE, gchar - 1) == FALSE)) {
  390.             update(FALSE);
  391.             mlwrite(TEXT102);
  392. /*                "[Bogus goto argument]" */
  393.         }
  394.     } else if (searchflag) {
  395.         if (forwhunt(FALSE, 0) == FALSE)
  396.             update(FALSE);
  397.     }
  398.  
  399. }
  400.  
  401. #if    WINDOW_MSWIN
  402. #define GETBASEKEY getbasekey
  403. static int PASCAL NEAR getbasekey()
  404. {
  405.     register int c;
  406.  
  407.     notquiescent = -1;  /* will be <= 0 only if getkey() is called
  408.                directly from editloop(). This is used to
  409.                restrict some windows-specific actions
  410.                (menus, sizing, etc...) when not called from
  411.                the lowest level of the editor */
  412.     c = getkey();
  413.     notquiescent = 1;
  414.     return c;
  415. }
  416. #else
  417. #define GETBASEKEY getkey
  418. #endif
  419.  
  420. /*
  421.     This is called to let the user edit something.    Note that if you
  422.     arrange to be able to call this from a macro, you will have
  423.     invented the "recursive-edit" function.
  424. */
  425.  
  426. PASCAL NEAR editloop()
  427.  
  428. {
  429.     register int c;        /* command character */
  430.     register int f;     /* default flag */
  431.     register int n;     /* numeric repeat count */
  432.     register int mflag;    /* negative flag on repeat */
  433.     register int basec;    /* c stripped of meta character */
  434.     register int oldflag;    /* old last flag value */
  435.     char time[6];        /* current display time */
  436.  
  437.     /* setup to process commands */
  438.     lastflag = 0;        /* Fake last flags.    */
  439.  
  440. loop:
  441.     /* if a macro error is pending, wait for a character */
  442.     if (exec_error) {
  443. #if    WINDOW_MSWIN
  444.         mlhistory();
  445. #else
  446.         mlforce(TEXT227);
  447. /*            "\n--- Press any key to Continue ---" */
  448.         tgetc();
  449. #endif
  450.         sgarbf = TRUE;
  451.         update(FALSE);
  452.         mlferase();
  453.         exec_error = FALSE;
  454.     }
  455.  
  456.     /* if we were called as a subroutine and want to leave, do so */
  457.     if (eexitflag)
  458.         return(eexitval);
  459.  
  460.     /* execute the "command" macro...normally null */
  461.     oldflag = lastflag;    /* preserve lastflag through this */
  462.     execkey(&cmdhook, FALSE, 1);
  463.     lastflag = oldflag;
  464.  
  465.     /* Notify user of any intercepted system messages.
  466.      * VMS only right now.
  467.      */
  468. #if    VMS
  469.     if (pending_msg) {
  470.         pending_msg = FALSE;
  471.         echostring(brdcstbuf, 0, NSTRING);
  472.     }
  473. #endif
  474.  
  475.     /* update time on the bottom modeline? */
  476.     if (timeflag)
  477. #if TYPEAH || WINDOW_MSWIN
  478.         if (!typahead())
  479. #endif
  480.         {
  481.             getdtime(time);
  482.             if (strcmp(lasttime, time) != 0)
  483.                 upmode();
  484.         }
  485.  
  486.     /* update position on current modeline? */
  487.     if (posflag)
  488. #if TYPEAH || WINDOW_MSWIN
  489.         if (!typahead())
  490. #endif
  491.         upmode();
  492.  
  493.     /* Fix up the screen    */
  494.     update(FALSE);
  495.  
  496.     /* get the next command from the keyboard */
  497.     discmd = TRUE;
  498.     disinp = TRUE;
  499.     c = GETBASEKEY();
  500.  
  501.     /* if there is something on the command line, clear it */
  502.     if (mpresf != FALSE) {
  503.         mlerase();
  504.         update(FALSE);
  505.     }
  506.  
  507.     /* override the arguments if prefixed */
  508.     if (prefix) {
  509.         if (is_lower(c & 255))
  510.             c = (c & ~255) | upperc(c & 255);
  511.         c |= prefix;
  512.         f = predef;
  513.         n = prenum;
  514.         prefix = 0;
  515.     } else {
  516.         f = FALSE;
  517.         n = 1;
  518.     }
  519.  
  520.     /* do META-# processing if needed */
  521.  
  522.     basec = c & ~META;        /* strip meta char off if there */
  523.     if ((c & META) && ((basec >= '0' && basec <= '9') || basec == '-') &&
  524.         (getbind(c) == NULL)) {
  525.         f = TRUE;        /* there is a # arg */
  526.         n = 0;            /* start with a zero default */
  527.         mflag = 1;        /* current minus flag */
  528.         c = basec;        /* strip the META */
  529.         while ((c >= '0' && c <= '9') || (c == '-')) {
  530.             if (c == '-') {
  531.                 /* already hit a minus or digit? */
  532.                 if ((mflag == -1) || (n != 0))
  533.                     break;
  534.                 mflag = -1;
  535.             } else {
  536.                 n = n * 10 + (c - '0');
  537.             }
  538.             if ((n == 0) && (mflag == -1))    /* lonely - */
  539.                 mlwrite("Arg:");
  540.             else
  541.                 mlwrite("Arg: %d",n * mflag);
  542.  
  543.             c = GETBASEKEY();    /* get the next key */
  544.         }
  545.         n = n * mflag;    /* figure in the sign */
  546.     }
  547.  
  548.     /* do ^U repeat argument processing */
  549.  
  550.     if (c == reptc) {           /* ^U, start argument   */
  551.         f = TRUE;
  552.         n = 4;                /* with argument of 4 */
  553.         mflag = 0;            /* that can be discarded. */
  554.         mlwrite("Arg: 4");
  555.         while ((c=GETBASEKEY()) >='0' && c<='9' || c==reptc || c=='-') {
  556.             if (c == reptc)
  557.                 if ((n > 0) == ((n*4) > 0))
  558.                     n = n*4;
  559.                 else
  560.                     n = 1;
  561.             /*
  562.              * If dash, and start of argument string, set arg.
  563.              * to -1.  Otherwise, insert it.
  564.              */
  565.             else if (c == '-') {
  566.                 if (mflag)
  567.                     break;
  568.                 n = 0;
  569.                 mflag = -1;
  570.             }
  571.             /*
  572.              * If first digit entered, replace previous argument
  573.              * with digit and set sign.  Otherwise, append to arg.
  574.              */
  575.             else {
  576.                 if (!mflag) {
  577.                     n = 0;
  578.                     mflag = 1;
  579.                 }
  580.                 n = 10*n + c - '0';
  581.             }
  582.             mlwrite("Arg: %d", (mflag >=0) ? n : (n ? -n : -1));
  583.         }
  584.         /*
  585.          * Make arguments preceded by a minus sign negative and change
  586.          * the special argument "^U -" to an effective "^U -1".
  587.          */
  588.         if (mflag == -1) {
  589.             if (n == 0)
  590.                 n++;
  591.             n = -n;
  592.         }
  593.     }
  594.  
  595.     /* and execute the command */
  596.     execute(c, f, n);
  597.     goto loop;
  598. }
  599.  
  600. /*
  601.  * Initialize all of the buffers, windows and screens. The buffer name is
  602.  * passed down as an argument, because the main routine may have been told
  603.  * to read in a file by default, and we want the buffer name to be right.
  604.  */
  605.  
  606. PASCAL NEAR edinit(bname)
  607.  
  608. char bname[];    /* name of buffer to initialize */
  609.  
  610. {
  611.     register BUFFER *bp;
  612.     register int index;
  613.  
  614.     /* init the kill ring */
  615.     for (index = 0; index < NRING; index++) {
  616.         kbufp[index] = (KILL *)NULL;
  617.         kbufh[index] = (KILL *)NULL;
  618.         kskip[index] = 0;
  619.         kused[index] = KBLOCK;
  620.     }
  621.     kill_index = 0;
  622.  
  623.     /* initialize some important globals */
  624.  
  625.     readhook.k_ptr.fp = nullproc;    /* set internal hooks to OFF */
  626.     readhook.k_type = BINDFNC;
  627.     wraphook.k_ptr.fp = wrapword;
  628.     wraphook.k_type = BINDFNC;
  629.     cmdhook.k_ptr.fp = nullproc;
  630.     cmdhook.k_type = BINDFNC;
  631.     writehook.k_ptr.fp = nullproc;
  632.     writehook.k_type = BINDFNC;
  633.     bufhook.k_ptr.fp = nullproc;
  634.     bufhook.k_type = BINDFNC;
  635.     exbhook.k_ptr.fp = nullproc;
  636.     exbhook.k_type = BINDFNC;
  637.  
  638.     /* allocate the first buffer */
  639.     bp = bfind(bname, TRUE, 0);        /* First buffer     */
  640.     blistp = bfind("[Buffers]", TRUE, BFINVS); /* Buffer list buffer    */
  641.     slistp = bfind("[Screens]", TRUE, BFINVS); /* Buffer list buffer    */
  642.     if (bp==NULL || blistp==NULL)
  643.         meexit(1);
  644.  
  645.     /* and allocate the default screen */
  646.     first_screen = (SCREEN *)NULL;
  647.     init_screen("MAIN", bp);
  648.     if (first_screen == (SCREEN *)NULL)
  649.         meexit(1);
  650.  
  651.     /* set the current default screen/buffer/window */
  652.     curbp = bp;
  653.     curwp = wheadp = first_screen->s_cur_window = first_screen->s_first_window;
  654. }
  655.  
  656. /*
  657.  * This is the general command execution routine. It handles the fake binding
  658.  * of all the keys to "self-insert". It also clears out the "thisflag" word,
  659.  * and arranges to move it to the "lastflag", so that the next command can
  660.  * look at it. Return the status of command.
  661.  */
  662. PASCAL NEAR execute(c, f, n)
  663.  
  664. int c;        /* key to execute */
  665. int f;        /* prefix argument flag */
  666. int n;        /* prefix value */
  667.  
  668. {
  669.     register int status;
  670.     KEYTAB *key;        /* key entry to execute */
  671. #if    DBCS
  672.     int schar;        /* second key in 2 byte sequence */
  673. #endif
  674.  
  675. #if    WINDOW_MSWIN
  676.     /* if it is a menu command, go for it... */
  677.     if ((c & MENU) == MENU) {
  678.         thisflag = 0;
  679.         status = execmenu(f, n);
  680.         lastflag = thisflag;
  681.         return(status);
  682.     }
  683. #endif
  684.     /* if the keystroke is a bound function...do it */
  685.     key = getbind(c);
  686.     if (key != NULL) {
  687.  
  688.         /* Don't reset the function type flags on a prefix */
  689.         if ((key->k_type == BINDFNC) &&
  690.             ((key->k_ptr.fp == meta) || (key->k_ptr.fp == cex)))
  691.             status = execkey(key, f, n);
  692.         else {
  693.             thisflag = 0;
  694.             status = execkey(key, f, n);
  695.             lastflag = thisflag;
  696.         }
  697.  
  698.         return(status);
  699.     }
  700.  
  701.     /*
  702.      * If a space was typed, fill column is defined, the argument is non-
  703.      * negative, wrap mode is enabled, and we are now past fill column,
  704.      * and we are not read-only, perform word wrap.
  705.      */
  706.     if (c == ' ' && (curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  707.         n >= 0 && getccol(FALSE) > fillcol &&
  708.         (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  709.         execkey(&wraphook, FALSE, 1);
  710.  
  711.     if ((c>=0x20 && c<=0xFF)) {    /* Self inserting.    */
  712.         if (n <= 0) {            /* Fenceposts.        */
  713.             lastflag = 0;
  714.             return(n<0 ? FALSE : TRUE);
  715.         }
  716.         thisflag = 0;            /* For the future.    */
  717.  
  718.         /* replace or overwrite mode, not at the end of a string */
  719.         if (curwp->w_bufp->b_mode & (MDREPL | MDOVER) &&
  720.             curwp->w_doto < lused(curwp->w_dotp)) {
  721.  
  722.             /* if we are in replace mode, or
  723.                (next char is not a tab or we are at a tab stop) */
  724.             if (curwp->w_bufp->b_mode & MDREPL ||
  725.                 (lgetc(curwp->w_dotp, curwp->w_doto) != '\t' ||
  726.                 getccol(FALSE) % tabsize == (tabsize - 1)))
  727.                         ldelete(1L, FALSE);
  728.         }
  729.  
  730.         /* do the appropriate insertion */
  731.         if (c == '}' && (curbp->b_mode & MDCMOD) != 0)
  732.             status = insbrace(n, c);
  733.         else if (c == '#' && (curbp->b_mode & MDCMOD) != 0)
  734.             status = inspound();
  735. #if    DBCS
  736.         else if (is2char(c)) {
  737.             schar = getkey();
  738.             status = TRUE;
  739.             while (n--) {
  740.                 if (linsert(1, c) == FALSE)
  741.                     status = FALSE;
  742.                 if (linsert(1, schar) == FALSE)
  743.                     status = FALSE;
  744.             }
  745.         }
  746. #endif
  747.  
  748.         else
  749.             status = linsert(n, c);
  750.  
  751.         /* check for CMODE fence matching */
  752.         if ((c == '}' || c == ')' || c == ']') &&
  753.                 (curbp->b_mode & MDCMOD) != 0)
  754.             fmatch(c);
  755.  
  756.         /* check auto-save mode */
  757.         if (curbp->b_mode & MDASAVE)
  758.             if (--gacount == 0) {
  759.                 /* and save the file if needed */
  760.                 upscreen(FALSE, 0);
  761.                 filesave(FALSE, 0);
  762.                 gacount = gasave;
  763.             }
  764.  
  765.         lastflag = thisflag;
  766.         return(status);
  767.     }
  768.     TTbeep();
  769.     mlwrite(TEXT19);        /* complain        */
  770. /*        "[Key not bound]" */
  771.     lastflag = 0;                /* Fake last flags.    */
  772.     return(FALSE);
  773. }
  774.  
  775. /*
  776.     Fancy quit command, as implemented by Norm. If the any buffer
  777. has changed do a write on that buffer and exit emacs, otherwise simply
  778. exit.
  779. */
  780.  
  781. PASCAL NEAR quickexit(f, n)
  782.  
  783. int f,n;    /* prefix flag and argument */
  784.  
  785. {
  786.     register BUFFER *bp;    /* scanning pointer to buffers */
  787.     register BUFFER *oldcb; /* original current buffer */
  788.     register int status;
  789.  
  790.     oldcb = curbp;                /* save in case we fail */
  791.  
  792. #if    TIPC
  793.     mlwrite("\n\n");
  794. #endif
  795.     bp = bheadp;
  796.     while (bp != NULL) {
  797.         if ((bp->b_flag&BFCHG) != 0    /* Changed.        */
  798.         && (bp->b_flag&BFINVS) == 0) {    /* Real.        */
  799.             curbp = bp;        /* make that buffer cur */
  800.             mlwrite(TEXT103,bp->b_fname);
  801. /*                "[Saving %s]" */
  802.             mlwrite("\n");
  803.             if ((status = filesave(f, n)) != TRUE) {
  804.                 curbp = oldcb;    /* restore curbp */
  805.                 return(status);
  806.             }
  807.         }
  808.     bp = bp->b_bufp;            /* on to the next buffer */
  809.     }
  810.     quit(f, n);                /* conditionally quit    */
  811.     return(TRUE);
  812. }
  813.  
  814. /*
  815.  * Quit command. If an argument, always quit. Otherwise confirm if a buffer
  816.  * has been changed and not written out. Normally bound to "C-X C-C".
  817.  */
  818.  
  819. PASCAL NEAR quit(f, n)
  820.  
  821. int f,n;    /* prefix flag and argument */
  822.  
  823. {
  824.     register int status;    /* return status */
  825.  
  826.     if (f != FALSE        /* Argument forces it.    */
  827.     || anycb() == FALSE    /* All buffers clean or user says it's OK. */
  828.     || (status = mlyesno(TEXT104)) == TRUE) {
  829. /*                 "Modified buffers exist. Leave anyway" */
  830. #if    FILOCK
  831.         if (lockrel() != TRUE) {
  832.             TTputc('\n');
  833.             TTputc('\r');
  834.             TTclose();
  835.             TTkclose();
  836.             status = meexit(1);
  837.         }
  838. #endif
  839.         if (f)
  840.             status = meexit(n);
  841.         else
  842.             status = meexit(GOOD);
  843.     }
  844.     mlerase();
  845.     return(status);
  846. }
  847.  
  848. PASCAL NEAR meexit(status)
  849. int status;    /* return status of emacs */
  850. {
  851.     eexitflag = TRUE;    /* flag a program exit */
  852.     eexitval = status;
  853.  
  854.     /* and now.. we leave and let the main loop kill us */
  855.     return(TRUE);
  856. }
  857.  
  858. /*
  859.  * Begin a keyboard macro.
  860.  * Error if not at the top level in keyboard processing. Set up variables and
  861.  * return.
  862.  */
  863.  
  864. PASCAL NEAR ctlxlp(f, n)
  865.  
  866. int f,n;    /* prefix flag and argument */
  867.  
  868. {
  869.     if (kbdmode != STOP) {
  870.         mlwrite(TEXT105);
  871. /*            "%%Macro already active" */
  872.         return(FALSE);
  873.     }
  874.     mlwrite(TEXT106);
  875. /*        "[Start macro]" */
  876.     kbdptr = &kbdm[0];
  877.     kbdend = kbdptr;
  878.     kbdmode = RECORD;
  879.     return(TRUE);
  880. }
  881.  
  882. /*
  883.  * End keyboard macro. Check for the same limit conditions as the above
  884.  * routine. Set up the variables and return to the caller.
  885.  */
  886.  
  887. PASCAL NEAR ctlxrp(f, n)
  888.  
  889. int f,n;    /* prefix flag and argument */
  890.  
  891. {
  892.     if (kbdmode == STOP) {
  893.         mlwrite(TEXT107);
  894. /*            "%%Macro not active" */
  895.         return(FALSE);
  896.     }
  897.     if (kbdmode == RECORD) {
  898.         mlwrite(TEXT108);
  899. /*            "[End macro]" */
  900.         kbdmode = STOP;
  901.     }
  902.     return(TRUE);
  903. }
  904.  
  905. /*
  906.  * Execute a macro.
  907.  * The command argument is the number of times to loop. Quit as soon as a
  908.  * command gets an error. Return TRUE if all ok, else FALSE.
  909.  */
  910.  
  911. PASCAL NEAR ctlxe(f, n)
  912.  
  913. int f,n;    /* prefix flag and argument */
  914.  
  915. {
  916.     if (kbdmode != STOP) {
  917.         mlwrite(TEXT105);
  918. /*            "%%Macro already active" */
  919.         return(FALSE);
  920.     }
  921.     if (n <= 0)
  922.         return(TRUE);
  923.     kbdrep = n;        /* remember how many times to execute */
  924.     kbdmode = PLAY;     /* start us in play mode */
  925.     kbdptr = &kbdm[0];    /*    at the beginning */
  926.     return(TRUE);
  927. }
  928.  
  929. /*
  930.  * Abort.
  931.  * Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
  932.  * Sometimes called as a routine, to do general aborting of stuff.
  933.  */
  934.  
  935. PASCAL NEAR ctrlg(f, n)
  936.  
  937. int f,n;    /* prefix flag and argument */
  938.  
  939. {
  940.     TTbeep();
  941.     kbdmode = STOP;
  942.     mlwrite(TEXT8);
  943. /*        "[Aborted]" */
  944.     return(ABORT);
  945. }
  946.  
  947. /* tell the user that this command is illegal while we are in
  948.    VIEW (read-only) mode                */
  949.  
  950. PASCAL NEAR rdonly()
  951.  
  952. {
  953.     TTbeep();
  954.     mlwrite(TEXT109);
  955. /*        "[Key illegal in VIEW mode]" */
  956.     return(FALSE);
  957. }
  958.  
  959. PASCAL NEAR resterr()
  960.  
  961. {
  962.     TTbeep();
  963.     mlwrite(TEXT110);
  964. /*        "[That command is RESTRICTED]" */
  965.     return(FALSE);
  966. }
  967.  
  968. PASCAL NEAR nullproc(f, n)    /* user function that does NOTHING */
  969.  
  970. int n, f;    /* yes, these are default and never used.. but MUST be here */
  971.  
  972. {
  973. }
  974.  
  975. PASCAL NEAR meta(f, n)    /* set META prefixing pending */
  976.  
  977. int f, n;    /* prefix flag and argument */
  978.  
  979. {
  980.     prefix |= META;
  981.     prenum = n;
  982.     predef = f;
  983.     return(TRUE);
  984. }
  985.  
  986. PASCAL NEAR cex(f, n)    /* set ^X prefixing pending */
  987.  
  988. int f, n;    /* prefix flag and argument */
  989.  
  990. {
  991.     prefix |= CTLX;
  992.     prenum = n;
  993.     predef = f;
  994.     return(TRUE);
  995. }
  996.  
  997. PASCAL NEAR unarg()    /* dummy function for binding to universal-argument */
  998. {
  999. }
  1000.  
  1001. /*    bytecopy:    copy a string...with length restrictions
  1002.             ALWAYS null terminate
  1003. */
  1004.  
  1005. char *PASCAL NEAR bytecopy(dst, src, maxlen)
  1006.  
  1007. char *dst;    /* destination of copied string */
  1008. char *src;    /* source */
  1009. int maxlen;    /* maximum length */
  1010.  
  1011. {
  1012.     char *dptr;    /* ptr into dst */
  1013.  
  1014.     dptr = dst;
  1015.     while ((maxlen-- > 0) && *src)
  1016.         *dptr++ = *src++;
  1017.     *dptr = 0;
  1018.     return(dst);
  1019. }
  1020.  
  1021. /*    copystr:    make another copy of the argument
  1022.  
  1023. */
  1024.  
  1025. char *PASCAL NEAR copystr(sp)
  1026.  
  1027. char *sp;    /* string to copy */
  1028.  
  1029. {
  1030.     char *dp;    /* copy of string */
  1031.  
  1032.     /* make room! */
  1033.     dp = malloc(strlen(sp)+1);
  1034.     if (dp == NULL)
  1035.         return(NULL);
  1036.     strcpy(dp, sp);
  1037.     return(dp);
  1038. }
  1039.  
  1040. /*****        Compiler specific Library functions    ****/
  1041.  
  1042. #if    RAMSIZE
  1043. /*    These routines will allow me to track memory usage by placing
  1044.     a layer on top of the standard system malloc() and free() calls.
  1045.     with this code defined, the environment variable, $RAM, will
  1046.     report on the number of bytes allocated via malloc.
  1047.  
  1048.     with SHOWRAM defined, the number is also posted on the
  1049.     end of the bottom mode line and is updated whenever it is changed.
  1050. */
  1051.  
  1052. #undef    malloc
  1053. #undef    free
  1054.  
  1055. #if     VMS & OPTMEM        /* these routines are faster! */
  1056. #define    malloc    VAXC$MALLOC_OPT
  1057. #define free    VAXC$FREE_OPT
  1058. #endif
  1059.  
  1060. char *allocate(nbytes)    /* allocate nbytes and track */
  1061.  
  1062. unsigned nbytes;    /* # of bytes to allocate */
  1063.  
  1064. {
  1065.     char *mp;    /* ptr returned from malloc */
  1066.     char *malloc();
  1067.     FILE *track;    /* malloc track file */
  1068.  
  1069.     mp = malloc(nbytes);
  1070.  
  1071. #if    RAMTRCK
  1072.     track = fopen("emacs.log", "a");
  1073.     fprintf(track, "Allocating %u bytes at %u:%u\n", nbytes,
  1074.             FP_SEG(mp), FP_OFF(mp));
  1075.     fclose(track);
  1076. #endif
  1077.  
  1078.     if (mp) {
  1079. #if    MSC
  1080.         envram += nbytes;
  1081. #else
  1082.         envram += 1;
  1083. #endif
  1084. #if    RAMSHOW
  1085.         dspram();
  1086. #endif
  1087.     }
  1088.  
  1089.     return(mp);
  1090. }
  1091.  
  1092. release(mp)    /* release malloced memory and track */
  1093.  
  1094. char *mp;    /* chunk of RAM to release */
  1095.  
  1096. {
  1097.     unsigned *lp;    /* ptr to the long containing the block size */
  1098. #if    RAMTRCK
  1099.     FILE *track;    /* malloc track file */
  1100.  
  1101.     track = fopen("emacs.log", "a");
  1102.     fprintf(track, "Freeing %u:%u\n",
  1103.             FP_SEG(mp), FP_OFF(mp));
  1104.     fclose(track);
  1105. #endif
  1106.  
  1107.     if (mp) {
  1108.         /* update amount of ram currently malloced */
  1109. #if    MSC
  1110.         lp = ((unsigned *)mp) - 1;
  1111.         envram -= (long)*lp - 2;
  1112. #else
  1113.         envram -= 1;
  1114. #endif
  1115.         free(mp);
  1116. #if    RAMSHOW
  1117.         dspram();
  1118. #endif
  1119.     }
  1120. }
  1121.  
  1122. #if    RAMSHOW
  1123. dspram()    /* display the amount of RAM currently malloced */
  1124.  
  1125. {
  1126.     char mbuf[20];
  1127.     char *sp;
  1128.     FILE *track;    /* malloc track file */
  1129.  
  1130.     TTmove(term.t_nrow - 1, 70);
  1131. #if    COLOR
  1132.     TTforg(7);
  1133.     TTbacg(0);
  1134. #endif
  1135.     sprintf(mbuf, "[%lu]", envram);
  1136.     sp = &mbuf[0];
  1137.     while (*sp)
  1138.         TTputc(*sp++);
  1139.     TTmove(term.t_nrow, 0);
  1140.     movecursor(term.t_nrow, 0);
  1141. #if    RAMTRCK & LOGFLG
  1142.     track = fopen("emacs.log", "a");
  1143.     fprintf(track, "Total allocation at %lu bytes\n", envram);
  1144.     fprintf(track, "Stack space at %u bytes\n", stackavail());
  1145.     fclose(track);
  1146. #endif
  1147. }
  1148. #endif
  1149. #endif
  1150.